package com.university.fragment;

import java.lang.reflect.Field;

import android.R.integer;
import android.app.Fragment;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.SearchView;
import android.widget.SearchView.OnQueryTextListener;
import android.widget.TextView;

import com.example.myuniversity.R;
import com.university.utils.SqliteDatabaseManager;

public class GradeFragment extends Fragment {
	private View view;
	private ListView listView;
	private SearchView searchView;
	private RadioButton radioAll,radioWen,radioLi,radioQuanbu,radioTiqian,radioYiben,radioErben,radioSanben,radioZhuanke;
	private Cursor cursor;
	private LayoutInflater mInflater;
	private String subject,pici;
	private SQLiteDatabase mDatabase;
	private BaseAdapter adapter;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		view=inflater.inflate(R.layout.fragment_grade, container, false);
		mInflater=inflater;
		mDatabase=SqliteDatabaseManager.getInstance(getActivity()).getDatabase();
		init();
		setOnClick();
		adapter=new MyAdapter();
		listView.setAdapter(adapter);
		//设置searchview的提交按钮显示，默认不显示
		searchView.setSubmitButtonEnabled(true);
		//反射来获得searchView的自动完成框变量
        Class<?> argClass = searchView.getClass();     
	    //--指定某个私有属性,mSearchPlate是搜索框父布局的名字        
	    Field ownField;
		try {
			ownField = argClass.getDeclaredField("mSubmitButton");
			ownField.setAccessible(true);     
			ImageView imageSubmit = (ImageView) ownField.get(searchView);
			imageSubmit.setImageResource(android.R.drawable.ic_menu_search);
		} catch (NoSuchFieldException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (IllegalArgumentException e) {
			e.printStackTrace();
		}
		return view;
	}
	
	private void setOnClick() {
		radioAll.setOnClickListener(new onRadioClick());
		radioWen.setOnClickListener(new onRadioClick());
		radioLi.setOnClickListener(new onRadioClick());
		radioQuanbu.setOnClickListener(new onRadioClick());
		radioTiqian.setOnClickListener(new onRadioClick());
		radioYiben.setOnClickListener(new onRadioClick());
		radioErben.setOnClickListener(new onRadioClick());
		radioSanben.setOnClickListener(new onRadioClick());
		radioZhuanke.setOnClickListener(new onRadioClick());
		searchView.setOnQueryTextListener(new OnQueryTextListener() {
			@Override
			public boolean onQueryTextSubmit(String query) {
				int grade=Integer.valueOf(query);
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, 
						"文理 like ? and 批次 like ? and 最低分>"+(grade-3)+" and 最低分<"+(grade+3),
						new String[]{subject,pici}, "最低分,院校代码", null, null);
				adapter.notifyDataSetChanged();
				return false;
			}
			@Override
			public boolean onQueryTextChange(String newText) {
				if (newText.length()==0) {
					cursor.close();
					cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
							new String[]{subject,pici}, "院校代码,最低分", null, null);
				}
				adapter.notifyDataSetChanged();
				return false;
			}
		});
	}
	private void init() {
		subject="%";
		pici="%";
		cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, null, null, "院校代码,最低分", null, null);
		listView=(ListView) view.findViewById(R.id.listview_fragment_grade);
		searchView=(SearchView) view.findViewById(R.id.searchview_fragment_grade);
		radioAll=(RadioButton) view.findViewById(R.id.radio_fragment_grade_all);
		radioWen=(RadioButton) view.findViewById(R.id.radio_fragment_grade_wen);
		radioLi=(RadioButton) view.findViewById(R.id.radio_fragment_grade_li);
		radioQuanbu=(RadioButton) view.findViewById(R.id.radio_fragment_grade_quanbu);
		radioTiqian=(RadioButton) view.findViewById(R.id.radio_fragment_grade_tiqian);
		radioYiben=(RadioButton) view.findViewById(R.id.radio_fragment_grade_yiben);
		radioErben=(RadioButton) view.findViewById(R.id.radio_fragment_grade_erben);
		radioSanben=(RadioButton) view.findViewById(R.id.radio_fragment_grade_sanben);
		radioZhuanke=(RadioButton) view.findViewById(R.id.radio_fragment_grade_zhuanke);
	}
	class onRadioClick implements OnClickListener{
		@Override
		public void onClick(View v) {
			switch (v.getId()) {
			case R.id.radio_fragment_grade_all:
				subject="%";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_wen:
				subject="文科";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_li:
				subject="理科";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_quanbu:
				pici="%";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_tiqian:
				pici="%";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_yiben:
				pici="本一";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_erben:
				pici="本二";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_sanben:
				pici="本三";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			case R.id.radio_fragment_grade_zhuanke:
				pici="专科";
				cursor.close();
				cursor=mDatabase.query("院校表", new String[]{"院校代码","院校名称","最低分","文理","批次","选修"}, "文理 like ? and 批次 like ?",
						new String[]{subject,pici}, "院校代码,最低分", null, null);
				break;
			default:
				break;
			}
			adapter.notifyDataSetChanged();
		}
	}
	private class MyAdapter extends BaseAdapter{
		@Override
		public int getCount() {
			return cursor.getCount();
		}
		@Override
		public Object getItem(int position) {
			return cursor.moveToPosition(position);
		}
		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			cursor.moveToPosition(position);
			ViewHolder viewHolder;
			
			if (convertView==null) {
				viewHolder=new ViewHolder();
				convertView=mInflater.inflate(R.layout.layout_grade_listview_item, parent, false);
				viewHolder.uId=(TextView) convertView.findViewById(R.id.txt_grade_listview_item_uid);
				viewHolder.uName=(TextView) convertView.findViewById(R.id.txt_grade_listview_item_uname);
				viewHolder.uGrade=(TextView) convertView.findViewById(R.id.txt_grade_listview_item_grade);
				viewHolder.uSubject=(TextView) convertView.findViewById(R.id.txt_grade_listview_item_wenli);
				viewHolder.uPici=(TextView) convertView.findViewById(R.id.txt_grade_listview_item_pici);
				viewHolder.uXuanxiu=(TextView) convertView.findViewById(R.id.txt_grade_listview_item_xuanxiu);
				convertView.setTag(viewHolder);
			}else {
				viewHolder=(ViewHolder) convertView.getTag();
			}
			viewHolder.uId.setText(cursor.getString(0));
			viewHolder.uName.setText(cursor.getString(1));
			viewHolder.uGrade.setText(cursor.getString(2));
			viewHolder.uSubject.setText(cursor.getString(3));
			viewHolder.uPici.setText(cursor.getString(4));
			viewHolder.uXuanxiu.setText(cursor.getString(5));
			return convertView;
		}
		
	}
	static class ViewHolder{
		TextView uId,uName,uGrade,uSubject,uPici,uXuanxiu;
	}

}
